using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Zune.Forms
{
    /// <summary>
    /// A TabPage Control.
    /// </summary>
    public class TabPage
    {
        #region Private/Internal Members
        private List<Control> _controls = new List<Control>();
        internal TabControl _parent;
        private string _text = "TabPage";
        private bool _visible = false;
        private Color _backColor = Color.Black;
        private Viewport _savedViewPort;
        private MenuItem _mi;
        #endregion

        #region Constructors
        /// <summary>
        /// A TabPage.
        /// </summary>
        /// <param name="parent">The TabControl that owns this TabPage.</param>
        public TabPage(TabControl parent)
        {
            _parent = parent;
            _parent._tabPages.Add(this);
            _mi = new MenuItem(_parent._contextMenu);
            _mi.Value = this;
        }
        #endregion

        #region Internal Methods
        internal void  Draw(GameTime gameTime)
        {
            Vector2 _location = new Vector2(_parent._location.X, _parent._location.Y + _parent._headerHeight);
            Vector2 _size = new Vector2(_parent._size.X, _parent._location.Y + _parent._size.Y - _parent._headerHeight);
            Viewport vp = new Viewport();
            vp.X = (Int32)_location.X;
            vp.Y = (Int32)_location.Y;
            vp.Width = (Int32)_size.X;
            vp.Height = (Int32)_size.Y;
            _savedViewPort = _parent._form.GraphicsDevice.Viewport;
            _parent._form.GraphicsDevice.Viewport = vp;

            _parent._form.GraphicsDevice.Clear(_backColor);

            _parent._form.GraphicsDevice.Viewport = _savedViewPort;
        }

        internal void DisplayControls(bool display)
        {
            Form frm = null;
            if (display)
            {
                frm = _parent._form;
            }

            foreach (Control ctl in _controls)
            {
                ctl._visible = display;
                ctl._form = frm;
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Adds a control to the TabPage.
        /// </summary>
        /// <param name="control">The control to add.</param>
        public void AddControl(Control control)
        {
            //if control size.Y > 480  - _parent._headerHeight, we need to trim it to fit
            if (control.Size.Y > 480 - _parent._headerHeight)
            {
                control.Size = new Vector2(control.Size.X, 480 - _parent._headerHeight);
            }
            control.Location = new Vector2(control.Location.X + _parent._location.X, control.Location.Y + _parent._location.Y  + _parent._headerHeight);
            control.Visible = false;
            _controls.Add(control);
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets the Text of the TabPage.
        /// </summary>
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                _mi.Text = _text;
            }
        }

        /// <summary>
        /// Gets or sets the Color to use as the backcolor.
        /// </summary>
        public Color BackColor
        {
            get { return _backColor; }
            set { _backColor = value; }
        }
        #endregion
    }
}